﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormGetPipeSpacing : System.Windows.Forms.Form
	{
		public FormGetPipeSpacing()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			base.CenterToScreen();
			this.initParameterSetting();
		}

		public double PipeSpacing
		{
			get
			{
				return this.pipeSpacing;
			}
			set
			{
				this.pipeSpacing = value;
			}
		}

		public int SpacingType
		{
			get
			{
				return this.spacingType;
			}
			set
			{
				this.spacingType = value;
			}
		}

		public bool BeCaclInsulationThickness()
		{
			return this.ckbLayer.Checked;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			if (this.spacingType == 0)
			{
				this.pipeSpacing = double.Parse(this.hytbSpaceCenter.Text.Trim());
			}
			else
			{
				this.pipeSpacing = double.Parse(this.hytbSpaceSide.Text.Trim());
			}
			base.DialogResult = DialogResult.OK;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void initParameterSetting()
		{
			Dictionary<string, string> pipeArrangementSetting = this.userSetting.GetPipeArrangementSetting();
			this.hytbSpaceCenter.Text = this.userSetting.GetValueFromDic("CenterSpacing", "200", pipeArrangementSetting);
			this.hytbSpaceSide.Text = this.userSetting.GetValueFromDic("SideSpacing", "80", pipeArrangementSetting);
			this.spacingType = int.Parse(this.userSetting.GetValueFromDic("SpacingType", "0", pipeArrangementSetting));
			this.ckbLayer.Checked = Convert.ToBoolean(this.userSetting.GetValueFromDic("Layer", "false", pipeArrangementSetting));
			if (this.spacingType == 0)
			{
				this.rbtPipeCenter.Checked = true;
				this.rbtPipeSide.Checked = false;
				return;
			}
			if (this.spacingType == 1)
			{
				this.rbtPipeCenter.Checked = false;
				this.rbtPipeSide.Checked = true;
			}
		}

		private void UserSettingWrite()
		{
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("SpacingType", this.spacingType.ToString());
				dictionary.Add("CenterSpacing", this.hytbSpaceCenter.Text.Trim());
				dictionary.Add("SideSpacing", this.hytbSpaceSide.Text.Trim());
				dictionary.Add("Layer", this.ckbLayer.Checked.ToString());
				this.userSetting.WritePipeArrangementSetting(dictionary);
			}
			catch (Exception)
			{
			}
		}

		private void rbtPipeCenter_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtPipeCenter.Checked)
			{
				this.label1.Enabled = true;
				this.hytbSpaceCenter.Enabled = true;
				this.ckbLayer.Enabled = false;
				this.spacingType = 0;
				return;
			}
			this.label1.Enabled = false;
			this.hytbSpaceCenter.Enabled = false;
			this.ckbLayer.Enabled = true;
			this.spacingType = 1;
		}

		private void rbtPipeSide_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtPipeSide.Checked)
			{
				this.label2.Enabled = true;
				this.hytbSpaceSide.Enabled = true;
				return;
			}
			this.label2.Enabled = false;
			this.hytbSpaceSide.Enabled = false;
		}

		private void hytbSpaceCenter_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbSpaceCenter.Text.Trim() != "")
			{
				if (!ControlJudgement.IsControlNumber(this.hytbSpaceCenter))
				{
					YJKMessageBox.Information("间距必须大于0！");
					this.hytbSpaceCenter.Undo();
					return;
				}
			}
			else
			{
				YJKMessageBox.Information("间距不能为空！");
				this.hytbSpaceCenter.Undo();
			}
		}

		private void hytbSpaceSide_Validating(object sender, CancelEventArgs e)
		{
			if (this.hytbSpaceSide.Text.Trim() != "")
			{
				if (!ControlJudgement.IsControlNumber(this.hytbSpaceSide))
				{
					YJKMessageBox.Information("间距必须大于0！");
					this.hytbSpaceSide.Undo();
					return;
				}
			}
			else
			{
				YJKMessageBox.Information("间距不能为空！");
				this.hytbSpaceSide.Undo();
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private double pipeSpacing = 3000.0;

		private int spacingType;
	}
}
